<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="zh-cn" style="height: 100%">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">
    <title><?=VIEW_TITLE?></title>
    <link href="<?php echo base_url('/assets/bs/'); ?>css/bootstrap.min.css" rel="stylesheet">
    <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <style></style>
</head>
<body>
<?php $this->load->view('simple_header.php'); ?>
<div class="container-fluid">
    <div id="main_div" class="row" style="">
        <div class="title" style="line-height:70px;font-size:36px;text-align: center;"><?=VIEW_TITLE?>密码修改</div>
        <div class="col-sm-4 col-md-4">
        </div>
        <div class="col-sm-4 col-md-4" id="cp_1">
            <form class="form-horizontal" method="post" onsubmit="return false;">
                <div class="form-group">
                    <div class="col-sm-12 col-md-12">
                        <input type="text" class="form-control" id="username" placeholder="请输入注册时所用手机号或者邮箱"
                               name="password">
                        <div class="col-sm-6 col-md-6" style="padding: 0">
                            <input style="margin-top: 10px;" type="authCode" class="form-control"
                                   id="authCode"
                                   placeholder="请输入验证码" name="authCode">
                        </div>
                        <div class="col-sm-4 col-xs-4">
                            <div id="authcode" style="margin-top: 10px;">

                            </div>
                        </div>
                        <div class="col-sm-2 col-xs-2">
                            <span onclick="getAuthCode();" class="glyphicon glyphicon-refresh"
                                  style="margin-top: 20px;"></span>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-12 col-md-12" style="margin-top: 10px;text-align: center;">
                        <button style="width: 49.2%" class="btn btn-success" onclick="codeAttestation()">下一步</button>
                    </div>
                </div>
            </form>
            <div class="alert alert-danger" role="alert" id="warn" style="display: none;"></div>
        </div>

        <div class="col-sm-4 col-md-4" id="cp_2" style="display: none;">
            <form class="form-horizontal" method="post" onsubmit="return false;">
                <div class="form-group">
                    <div class="col-sm-12 col-md-12">
                        <div class="input-group">
                            <input type="text" class="form-control" id="username_2" name="username"
                                   placeholder="请输入注册时所用手机号或者邮箱" readonly="readonly">
                            <span class="input-group-btn">
                                <button id="btn_getCode" class="btn btn-primary" type="button" onclick="getCode()">
                                    获取验证码
                                </button></span>
                        </div>
                        <input style="margin-top: 10px" type="text" class="form-control" id="code"
                               placeholder="请输入验证码" name="code">
                    </div>

                </div>
                <div class="form-group">
                    <div class="col-sm-12 col-md-12" style="margin-top: 10px;text-align: center;">
                        <button style="width: 49.2%" class="btn btn-success" onclick="codeAttestation2()">下一步</button>
                    </div>
                </div>
            </form>
            <div class="alert alert-danger" role="alert" id="warn" style="display: none;"></div>
        </div>

        <div class="col-sm-4 col-md-4" id="cp_3" style="display: none;">
            <form class="form-horizontal" method="post" onsubmit="return false;">
                <div class="form-group">
                    <div class="col-sm-12 col-md-12">
                        <input type="password" class="form-control" id="password_3_1" placeholder="请输入你的新密码"
                               name="password_3_1">
                        <input style="margin-top: 10px;" type="password" class="form-control"
                               id="password_3_2"
                               placeholder="请再次输入你的新密码" name="password_3_2">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-12 col-md-12" style="margin-top: 10px;text-align: center;">
                        <button style="width: 49.2%" class="btn btn-success" onclick="cpassword()">下一步</button>
                    </div>
                </div>
            </form>
            <div class="alert alert-danger" role="alert" id="warn" style="display: none;"></div>
        </div>
        <div class="col-sm-4 col-md-4" id="cp_4" style="display: none;text-align: center;">
            <h3>恭喜你,密码修改成功!</h3>
            <a style="font-size: 16px" class="" href="<?php echo site_url('/admin/login'); ?>">立即登录</a>
        </div>
        <div class="col-sm-4 col-md-4">
        </div>
    </div>
</div>
<?php
$this->load->view('footer.php');
?>
<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
<script src="<?php echo base_url('/assets/bs/'); ?>js/bootstrap.min.js"></script>
<script>
    var w_width = document.documentElement.clientWidth;
    var w_height = document.documentElement.clientHeight;
    var main_div_height = w_height - 300;
    $('#main_div').height(main_div_height + 'px');
    $('.title').css('margin-top', main_div_height * 0.1 + 'px');
    var auth_code = '';
    var pa_1 = 0;
    function getAuthCode() {
        $.ajax({
            url: '<?php echo site_url('/admin/getAuthCode')?>',
            type: 'post',
            dataType: 'json',
            data: {},
            success: function (data) {
                $('#authcode').empty();
                $('#authcode').append(data.image);
                auth_code = data.word;
            }
        });
    }
    getAuthCode();
    function codeAttestation() {
        var authCode = $('#authCode').val();
        var username = $('#username').val();
        if (username == '') {
            alert('账号不能留空!');
            return false;
        }
        if (auth_code != authCode) {
            alert('验证码错误!');
            return false;
        }

        $.ajax({
            url: '<?php echo site_url('/admin/cpAuthCodeAttestation')?>',
            type: 'post',
            dataType: 'json',
            data: {
                username: username,
                auth_code: auth_code
            },
            success: function (data) {
                if (data == 1) {
                    pa_1 = 1;
                    $("#username_2").val(username);
                    $('.title').text('手机或邮箱验证!');
                    $('#cp_1').hide();
                    $('#cp_2').show();

                } else if (data == 2) {
                    alert('该账户不存在!');
                }
            }
        });
    }


    function getCode() {
        $('#btn_getCode').attr('disabled', 'disabled');
        var username = $('#username_2').val();
        $.ajax({
            url: '<?php echo site_url('/Admin/getCode')?>',
            type: 'post',
            dataType: 'json',
            data: {
                username: username,
                codetype: 2,
                type: 'cpassword'
            },
            success: function (data) {
                console.log(data);
                if (data == 1) {
                    is_getCode = 1;
                    alert('验证码发送成功,请注意查收!');
                } else {
                    $('#btn_getCode').attr('disabled', 'none');
                    alert('验证码发送失败!');
                }
            }
        });
    }

    function codeAttestation2() {
        var username = $('#username_2').val();
        var code = $('#code').val();
        if (username == '' || code == '') {
            alert('请填写验证码!');
            return false;
        }
        $.ajax({
            url: '<?php echo site_url('/Admin/cpCodeAttestation')?>',
            type: 'post',
            dataType: 'json',
            data: {
                username: username,
                code: code
            },
            success: function (data) {
                if (data.code == 3) {
                    $('.title').text('新密码设置!');
                    $('#cp_1').hide();
                    $('#cp_2').hide();
                    $('#cp_3').show();
                } else {
                    alert(data['msg']);
                }
                console.log(data);
            }
        });
    }


    function cpassword() {
        var password1 = $('#password_3_1').val();
        var password2 = $('#password_3_2').val();
        if (password1 == '' || password2 == '') {
            return false;
        }
        if (password1 != password2) {
            $('#password_3_2').css('background-color', '#f2dede');
            return false;
        }
        $.ajax({
            url: '<?php echo site_url('/Admin/cpasswordChange')?>',
            type: 'post',
            dataType: 'json',
            data: {
                password1: password1,
                password2: password2
            },
            success: function (data) {
                console.log(data);
                if (data == 1) {
                    $('.title').text('设置成功!');
                    $('#cp_1').hide();
                    $('#cp_2').hide();
                    $('#cp_3').hide();
                    $('#cp_4').show();
                } else {
                    alert('密码修改失败,请勿填写旧密码或重试!');
                }
            }
        });
    }
</script>
</body>
</html>